package com.shundr.shipper.truck;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;

import com.shundr.shipper.common.util.ToastHelper;
import com.shundr.shipper.frame.util.StringUtil;
import com.shundr.shipper.truck.DialogCallPhone.OnCallPhoneListener;
import com.shundr.shipper.user.http.UserRequest;

/**
 * 给车主打电话公共类
 * 
 * @author Administrator
 *
 */
public class ContactTrucker {
	private Context mContext;
	String mPhone[];
	int nums;
	private OnCallListener callListener;

	public interface OnCallListener{
		public void onCall();
	}
	public ContactTrucker(Context context) {
		this.mContext = context;
		getQueryNumValidation();
	}

	@SuppressLint("HandlerLeak")
	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case UserRequest.GET_QueryNumValidationByUser_SUCCESSED:
				try {
					String num = (String) msg.obj;
					if (StringUtil.isNumeric(num)) {
						nums = Integer.valueOf(num);
						DialogCallPhone callPhone = new DialogCallPhone(mContext, nums,new OnCallPhoneListener() {
							
							@Override
							public void onSecond() {
								callListener.onCall();
							}
						});
						callPhone.show();
					} else {
						ToastHelper.toastShort(mContext, "获取失败");    
					}
				} catch (NumberFormatException e) {
					e.printStackTrace();
				}
				break;
			case UserRequest.REQUEST_FAILED:
				try {
					String description = (String) msg.obj;
					ToastHelper.toastShort(mContext, description);
				} catch (Exception e) {
					e.printStackTrace();
				}
				break;
			default:
				break;
			}
			super.handleMessage(msg);
		}

	};

	/***
	 * 获取可打电话次数
	 */
	private void getQueryNumValidation() {
		new UserRequest(mContext, mHandler).getQueryNumValidationByUser();
	}

	public void setCallListener(OnCallListener callListener) {
		this.callListener = callListener;
	}

}



